package com.hgkj.cloud.customer.util;

import com.hgkj.cloud.base.service.RemoteService;
import com.hgkj.cloud.base.util.FileUtil;
import com.hgkj.cloud.customer.common.FileSaver;
import com.hgkj.cloud.customer.common.ResponseData;
import com.hgkj.cloud.customer.file.FileSourceGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ：HMM
 * @date ：Created in 2022/1/12 13:07
 * @description：用来上传文件到指定目录
 */
public class UploadFileUtil {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public ResponseData uploadFileToServer(String uploadFilePath, String uploadFileServer, String prefixUrl,
                                           RemoteService remoteService, FileSourceGenerator fsg, MultipartFile uploadFile, String dir, String fileName) throws ParseException {
        ResponseData responseData = new ResponseData();
        List<String> urlList = new ArrayList<String>();
        Map<String, Object> map = new HashMap<String, Object>();
        try {

            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
            if (uploadFile != null) {
                StringBuffer misb = new StringBuffer("");
                if (!uploadFile.isEmpty()) {
                    if ("yun".equals(uploadFileServer)) {
                        FileSaver fs = new FileSaver(uploadFile);
                        String localPath = fsg.generator(fs);
                        String url = "";
                        url = remoteService.processCommonFile(localPath, "cusresources");
                        urlList.add(url);
                        String[] urlStr = urlList.toArray(new String[]{});
                        FileUtil.uploadFileToServer(urlStr, null, prefixUrl);
                    } else {
                        //存储路径
                        String storePath = uploadFilePath + dir + "/";
                        logger.info("========storePath==========" + storePath);
                        File filedir = new File(storePath);
                        if (!filedir.isDirectory()) {
                            filedir.mkdirs();
                        }
                        uploadFile.transferTo(new File(storePath + fileName));
                        fileName = dir + "/" + fileName;
                        logger.info("========fileName==========" + fileName);
                        urlList.add(fileName);
                        Set<PosixFilePermission> perms = new HashSet<>();
                        perms.add(PosixFilePermission.OWNER_READ);
                        perms.add(PosixFilePermission.OWNER_WRITE);
                        perms.add(PosixFilePermission.GROUP_READ);
                        perms.add(PosixFilePermission.OTHERS_READ);
                        logger.info("=========最终文件路径==========" + uploadFilePath + "======" + fileName);
                        Files.setPosixFilePermissions(Paths.get(uploadFilePath + fileName), perms);
                    }
                }
                map.put("fileList", urlList);
                responseData.setData(map);
            } else {
                responseData.setCode(-1);
                responseData.setMsg("资源文件为空，请选择文件!");
                return responseData;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("=========报错==========" + e);
            responseData.setCode(1);
            responseData.setMsg("提交异常！");
        }
        return responseData;
    }
}
